class ParameterController < ApplicationController
  
  before_filter :authenticate
  
  def index
    @modules = Parameter.modules
  end
  
  def list
    @modules = Parameter.modules
    render :update do |page|
      page.replace_html "parameter_list" , :partial => 'list_modules'
      page.show "parameter_list"
      if params[:displayform] == 'none'
        page.hide "parameter_form"
      end
    end
    
  end
  
  def list_parameters
    @parameters = Parameter.find(:all, :conditions => ["module = ?",params[:module]])
    render :update do |page|
      page.replace_html "parameter_tr_#{params[:module]}" , {:partial => 'list_parameters', :locals => {:modul => params[:module]} }
      page.show "parameter_tr_#{params[:module]}"
    end
  end
  
  def form
    unless authenticate_check?('(ADM:ALL||PARAM:ALL||(PARAM:VIEW))')
      render :update do |page|
        page.replace_html "parameter_form" , :partial => '/auth/not_permission'
        page.show "parameter_form"
      end      
    else
      if params[:id]
        @parameter = Parameter.find(params[:id])
      else
        @parameter = Parameter.new
        @parameter.module = params[:modul]
      end
      render :update do |page|
        page.replace_html "parameter_form" , :partial => 'form'
        page.show "parameter_form"
      end      
    end
  end
  
  def alter
    unless authenticate_check?('(ADM:ALL||PARAM:ALL||(PARAM:VIEW&&PARAM:MODIFY))')
      render :update do |page|
        page.replace_html "parameter_form" , :partial => '/auth/not_permission'
        page.show "parameter_form"
      end      
    else
      @parameter = Parameter.find(params[:parameter][:id])
      if request.post?
        @parameter.attributes = params[:parameter]
        if @parameter.save
          redirect_to :action => "list", :displayform => "none"
        else
          render :update do |page|
            page.replace_html "parameter_form" , :partial => 'form'
            page.show "parameter_form"
          end
        end
      end
    end
  end
  
  def create
    unless authenticate_check?('(ADM:ALL||PARAM:ALL||(PARAM:VIEW&&PARAM:CREATE))')
        render :update do |page|
          page.replace_html "parameter_form" , :partial => '/auth/not_permission'
          page.show "parameter_form"
        end      
    else
        if params[:id]
          @parameter = Parameter.find(params[:id])
        else
          @parameter = Parameter.new
        end
        if request.post?
          @parameter.attributes = params[:parameter]
          @parameter.module = params[:parameter][:module]
          if @parameter.save
            redirect_to :action => "list", :displayform => "none"
          else
            render :update do |page|
              page.replace_html "parameter_form" , :partial => 'form'
              page.show "parameter_form"
            end
          end
       end
    end
  end
  
  def remove
    unless authenticate_check?('(ADM:ALL||PARAM:ALL||(PARAM:REMOVE&&PARAM:VIEW))')
      render :update do |page|
        page << "alert('Desculpe, você não tem permissão para remoção.')"
      end
    else
      if params[:id]
        @parameter = Parameter.find(params[:id])
        if @parameter.destroy
          redirect_to :action => "list", :displayform => "none"
        end
      end     
    end
  end
  
  protected
  
=begin
Filtro de autenticação do controler
=end
  def authenticate
    authenticate_check('(ADM:ALL||PARAM:VIEW||PARAM:ALL)','/')
  end
    
end
